package edu.yourl.persistence;

import java.util.List;

import com.db4o.query.Query;

import edu.yourl.domain.Favoritable;
import edu.yourl.domain.Url;

public class FavoritableRepository extends AbstractRepository implements
		IFavoritableRepository {

	private static final String FAVORITABLE_FIELD = "favoritableName";
	private static final String SEO_FIELD = "SEOFriendlyId";


	@Override
	public <T extends Favoritable> T findFavoritableByName(Class<T> clazz,
			String name) {
		return this.getObjectByPropertyValue(clazz, FAVORITABLE_FIELD, name);
	}


	@Override
	public <T extends Favoritable> T findFavoritableBySeoFriendlyId(
			Class<T> clazz, String seoFriendlyId) {
		return this.getObjectByPropertyValue(clazz, SEO_FIELD, seoFriendlyId);
	}


	@SuppressWarnings("unchecked")
	@Override
	public <T extends Favoritable> List<T> findAllFavoritablesByName(
			Class<T> clazz, String name) {
		Query query = getDb4oTemplate().query();
		query.constrain(clazz);
		if (clazz.equals(Url.class)) {
			query.descend("url").constrain(name).like();
		} else {
			query.descend("domain").constrain(name).like();
		}
		return query.execute();
	}
	
}